<?php
/*
===========================================================================

  Copyright (c) 2010-2012 DSPWeb Development Team

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see http://www.gnu.org/licenses/

  This file is part of DSPWeb source code

===========================================================================
*/

// Account related functions
function getAccountID($login) {
	global $dbhost, $dbuser, $dbpass, $dbname;
	
	$sql = new SQL;
	$sql->connect($dbhost,$dbuser,$dbpass,$dbname);
	
	$query = $sql->query("SELECT id FROM accounts WHERE login='$login'");
	$res = $sql->fetch_assoc($query);
	
	return $res['id'];
	
	$sql->close();
}

function getAccountType($accid) {
	global $dbhost, $dbuser, $dbpass, $dspdb;
	
	$sql = new SQL;
	$sql->connect($dbhost,$dbuser,$dbpass,$dbname);
	$query = $sql->query("SELECT type FROM priviledges WHERE accid='$accid'");
	$res = $sql->fetch_assoc($query);
	if ($res['type'] == 0) {
		return "Player";
	}
	elseif ($res['type'] == 1) {
		return "Game Master";
	}
	elseif ($res['type'] == 2) {
		return "Administrator";
	}
	else {
		return "Player";
	}
	
	$sql->close();
}

function getAccountName($accid) {
	global $dbhost, $dbuser, $dbpass, $dbname;
	
	$sql = new SQL;
	$sql->connect($dbhost,$dbuser,$dbpass,$dbname);
	$query = $sql->query("SELECT login FROM accounts WHERE id='$accid'");
	$res = $sql->fetch_assoc($query);
	
	if ($sql->num_rows($query) < 1) {
		return "";
	}
	else {
		return $res['login'];
	}
}

// Character related functions

function getCharGender($charid) {
	global $dbhost, $dbuser, $dbpass, $dbname;
	
	$sql = new SQL;
	$sql->connect($dbhost,$dbuser,$dbpass,$dbname);
	$query = $sql->query("SELECT race FROM char_look WHERE charid='$charid'");
	$res = $sql->fetch_assoc($query);
	
	switch ($res['race']) {
		case 1:
			$gender = "Male";
			break;
		case 2:
			$gender = "Female";
			break;
		case 3:
			$gender = "Male";
			break;
		case 4:
			$gender = "Female";
			break;
		case 5:
			$gender = "Male";
			break;
		case 6:
			$gender = "Female";
			break;
		case 7:
			$gender = "Female";
			break;
		case 8:
			$gender = "Male";
			break;
		default:
			$gender = "Male";
			break;
	}
	
	return $gender;
	$sql->close();
}

function getCharRace($charid) {
	global $dbhost, $dbuser, $dbpass, $dbname;
	
	$sql = new SQL;
	$sql->connect($dbhost,$dbuser,$dbpass,$dbname);
	$query = $sql->query("SELECT race FROM char_look WHERE charid='$charid'");
	$res = $sql->fetch_assoc($query);
	
	switch ($res['race']) {
		case 1:
			$race = "Hume";
			break;
		case 2:
			$race = "Hume";
			break;
		case 3:
			$race = "Elvaan";
			break;
		case 4:
			$race = "Elvaan";
			break;
		case 5:
			$race = "Tarutaru";
			break;
		case 6:
			$race = "Tarutaru";
			break;
		case 7:
			$race = "Mithra";
			break;
		case 8:
			$race = "Galka";
			break;
		default:
			$race = "Hume";
			break;
	}
	
	return $race;
	$sql->close();
}

function getCharJob($charid,$job) {
	global $dbhost, $dbuser, $dbpass, $dbname;
	
	switch ($job) {
		case "main":
			$j = "mjob";
			break;
		case "sub":
			$j = "sjob";
			break;
		default:
			$j = "mjob";
			break;
	}
	
	$sql = new SQL;
	$sql->connect($dbhost,$dbuser,$dbpass,$dbname);
	$query = $sql->query("SELECT $j FROM char_stats WHERE charid='$charid'");
	$res = $sql->fetch_assoc($query);

	$jb = $res[$j];
	
	$jobs = array(
	'1' => 'war',
	'2' => 'mnk',
	'3' => 'whm',
	'4' => 'blm',
	'5' => 'rdm',
	'6' => 'thf',
	'7' => 'pld',
	'8' => 'drk',
	'9' => 'bst',
	'10' => 'brd',
	'11' => 'rng',
	'12' => 'sam',
	'13' => 'nin',
	'14' => 'drg',
	'15' => 'smn',
	'16' => 'blu',
	'17' => 'cor',
	'18' => 'pup',
	'19' => 'dnc',
	'20' => 'sch');

	if ($jb == 0) {
		return "";
	} 
	else {
		return $jobs[$jb];
	}
	$sql->close();
}

function getJobLevel($charid,$job) {
	global $dbhost, $dbuser, $dbpass, $dbname;
	
	$sql = new SQL;
	$sql->connect($dbhost,$dbuser,$dbpass,$dbname);
	$res = $sql->query("SELECT $job FROM char_jobs WHERE charid='$charid'");
	$r = $sql->fetch_assoc($res);
	
	return $r[$job];
	$sql->close();
}

function getCharZone($charid) {
	global $dbhost, $dbuser, $dbpass, $dbname;
	
	$sql = new SQL;
	$sql->connect($dbhost,$dbuser,$dbpass,$dbname);
	$query = $sql->query("SELECT pos_zone FROM chars WHERE charid='$charid'");
	$res = $sql->fetch_assoc($query);
	
	return $res['pos_zone'];
	$sql->close();
}

function getCharFaction($charid) {
	global $dbhost, $dbuser, $dbpass, $dbname;
	
	$sql = new SQL;
	$sql->connect($dbhost,$dbuser,$dbpass,$dbname);
	
	$query = $sql->query("SELECT nation FROM chars WHERE charid='$charid'");
	$res = $sql->fetch_assoc($query);
	
	switch ($res['nation']) {
		case 0:
			$nation = "San D'Oria";
			break;
		case 1:
			$nation = "Bastok";
			break;
		case 2:
			$nation = "Windurst";
			break;
		default:
			$nation = "San D'Oria";
			break;
	}
	
	return $nation;
	$sql->close();
}

function getCharMP($charid) {
	global $dbhost,$dbuser,$dbpass,$dbname;
	
	$sql = new SQL;
	$sql->connect($dbhost,$dbuser,$dbpass,$dbname);
	
	$query = $sql->query("SELECT mp FROM char_stats WHERE charid='$charid'");
	$res = $sql->fetch_assoc($query);
	
	if ($sql->num_rows($query) < 1) {
		return 0;
	}
	else {
		return $res['mp'];
	}
	
	$sql->close();
}

function getCharHP($charid) {
	global $dbhost,$dbuser,$dbpass,$dbname;
	
	$sql = new SQL;
	$sql->connect($dbhost,$dbuser,$dbpass,$dbname);
	
	$query = $sql->query("SELECT hp FROM char_stats WHERE charid='$charid'");
	$res = $sql->fetch_assoc($query);
	
	if ($sql->num_rows($query) < 1) {
		return 0;
	}
	else {
		return $res['hp'];
	}
	
	$sql->close();
}

function getCharXP($charid,$job) {
	global $dbhost,$dbuser,$dbpass,$dbname;
	
	$sql = new SQL;
	$sql->connect($dbhost,$dbuser,$dbpass,$dbname);
	
	$query = $sql->query("SELECT $job FROM char_exp WHERE charid='$charid'");
	$res = $sql->fetch_assoc($query);
	
	if ($sql->num_rows($query) < 1) {
		return 0;
	}
	else {
		return $res[$job];
	}
	
	$sql->close();
}

function getCharGil($charid) {
	global $dbhost,$dbuser,$dbpass,$dbname;
	
	$sql = new SQL;
	$sql->connect($dbhost,$dbuser,$dbpass,$dbname);
	
	$query = $sql->query("SELECT quantity from char_inventory WHERE itemID='65535' AND charid='$charid'");
	$res = $sql->fetch_assoc($query);
	
	if ($sql->num_rows($query) < 1) {
		return 0;
	}
	else {
		return $res['quantity'];
	}
	
	$sql->close();
}

function getCharTitle($charid) {
	global $dbhost,$dbuser,$dbpass,$dbname,$dspdb;
	
	$sql = new SQL;
	$sql->connect($dbhost,$dbuser,$dbpass,$dbname);
	
	$query = $sql->query("SELECT title FROM char_stats WHERE charid='$charid'");
	$res = $sql->fetch_assoc($query);
	
	$id = $res['title'];
	
	$temp = new SQL;
	$conn2 = $temp->connect($dbhost, $dbuser, $dbpass, $dspdb);
	
	$query = $temp->query("SELECT title FROM titles WHERE id='$id'");
	$res = $temp->fetch_assoc($query);
	
	return ucwords(strtolower(str_replace("_"," ",$res['title'])));
	
	$sql->close();
	$temp->close();
}


function getCharRank($charid,$faction) {
	global $dbhost, $dbuser, $dbpass, $dbname;
	
	$sql = new SQL;
	$sql->connect($dbhost,$dbuser,$dbpass,$dbname);
	
	switch ($faction) {
		case "San D'Oria":
			$home = "rank_sandoria";
			break;
		case "Bastok":
			$home = "rank_bastok";
			break;
		case "Windurst":
			$home = "rank_windurst";
			break;
		default:
			$home = "rank_sandoria";
			break;
	}
	$query = $sql->query("SELECT $home FROM char_profile WHERE charid='$charid'");
	$res = $sql->fetch_assoc($query);
	
	return $res[$home];
	$sql->close();
}

function getCharEquipment($charid,$slot) {
	global $dbhost, $dbuser, $dbpass, $dbname;
	
	$sql = new SQL;
	$sql->connect($dbhost,$dbuser,$dbpass,$dbname);
	
	$query = $sql->query("SELECT $slot FROM char_equip WHERE charid='$charid'");
	$res = $sql->fetch_assoc($query);
	$slotid = $res[$slot];
	
	if ($slotid == 0) {
		return "<em>Nothing equipped</em>";
	}
	else {
		$query = $sql->query("SELECT itemid FROM char_inventory WHERE location='0' AND slot='$slotid'");
		$res = $sql->fetch_assoc($query);
		$itemid = $res['itemid'];
		
		return ucwords(getItemName($itemid));
	}
	
	$sql->close();
}

function getCharSkillLevel($charid,$skill) {
	global $dbhost, $dbuser, $dbpass, $dbname;
	
	$sql = new SQL;
	$sql->connect($dbhost,$dbuser,$dbpass,$dbname);
	
	switch ($skill) {
	case "hand2hand":
		$skillid = 1;
		break;
	case "dagger":
		$skillid = 2;
		break;
	case "sword":
		$skillid = 3;
		break;
	case "great sword":
		$skillid = 4;
		break;
	case "axe":
		$skillid = 5;
		break;
	case "great axe":
		$skillid = 6;
		break;
	case "scythe":
		$skillid = 7;
		break;
	case "polearm":
		$skillid = 8;
		break;
	case "katana":
		$skillid = 9;
		break;
	case "great katana":
		$skillid = 10;
		break;
	case "club":
		$skillid = 11;
		break;
	case "staff":
		$skillid = 12;
		break;
	case "archery":
		$skillid = 25;
		break;
	case "marksmanship":
		$skillid = 26;
		break;
	case "throwing":
		$skillid = 27;
		break;
	case "guarding":
		$skillid = 28;
		break;
	case "evasion":
		$skillid = 29;
		break;
	case "shield":
		$skillid = 30;
		break;
	case "parrying":
		$skillid = 31;
		break;
	}
	
	$query = $sql->query("SELECT value FROM char_skills WHERE skillid='$skillid' AND charid='$charid'");
	$res = $sql->fetch_assoc($query);
	
	if ($sql->num_rows($query) < 1) {
		return 0;
	}
	else {
		return number_format($res['value']/10);
	}
	
	$sql->close();
}

function getCharMagicLevel($charid,$magic) {
	global $dbhost,$dbuser,$dbpass,$dbname;
	
	$sql = new SQL;
	$sql->connect($dbhost,$dbuser,$dbpass,$dbname);
	
	switch ($magic) {
		case "divine":
			$skillid = 32;
			break;
		case "healing":
			$skillid = 33;
			break;
		case "enhancing":
			$skillid = 34;
			break;
		case "enfeebling":
			$skillid = 35;
			break;
		case "elemental":
			$skillid = 36;
			break;
		case "dark":
			$skillid = 37;
			break;
		case "summoning":
			$skillid = 38;
			break;
		case "singing":
			$skillid = 40;
			break;
		case "string":
			$skillid = 41;
			break;
		case "wind":
			$skillid = 35;
			break;
		case "enhancing":
			$skillid = 34;
			break;
		case "ninjitsu":
			$skillid = 39;
			break;
		case "blue":
			$skillid = 43;
			break;
		default:
			$skillid = 35;
			break;
	}
	
	$query = $sql->query("SELECT value FROM char_skills WHERE skillid='$skillid' AND charid='$charid'");
	$res = $sql->fetch_assoc($query);
	
	if ($sql->num_rows($query) < 1) {
		return 0;
	}
	else {
		return number_format($res['value']/10);
	}
	
	$sql->close();
}

function getCharCraftingLevel($charid,$craft) {
	global $dbhost,$dbuser,$dbpass,$dbname;
	
	$sql = new SQL;
	$sql->connect($dbhost,$dbuser,$dbpass,$dbname);
	
	switch ($craft) {
		case "Fishing":
			$skillid = 48;
			break;
		case "Woodworking":
			$skillid = 49;
			break;
		case "Smithing":
			$skillid = 50;
			break;
		case "Goldsmithing":
			$skillid = 51;
			break;
		case "Clothcraft":
			$skillid = 52;
			break;
		case "Leathercraft":
			$skillid = 53;
			break;
		case "Bonecraft":
			$skillid = 54;
			break;
		case "Alchemy":
			$skillid = 55;
			break;
		case "Cooking":
			$skillid = 56;
			break;
		case "Synergy":
			$skillid = 57;
			break;
		case "Riding":
			$skillid = 58;
			break;
		default:
			$skillid = 0;
			break;
	}
	
	$query = $sql->query("SELECT value FROM char_skills WHERE charid='$charid' AND skillid='$skillid'");
	$res = $sql->fetch_assoc($query);
	
	if ($sql->num_rows($query) < 1) {
		return 0;
	}
	else {
		return number_format($res['value']/10);
	}
	
	$sql->close();
}

function getCharFame($charid,$nation) {
	global $dbhost,$dbuser,$dbpass,$dbname;

	$sql = new SQL;
	$sql->connect($dbhost,$dbuser,$dbpass,$dbname);
	
	$query = $sql->query("SELECT fame_$nation FROM char_profile WHERE charid='$charid'");
	$res = $sql->fetch_assoc($query);
	
	return $res['fame_'.$nation];
	$sql->close();
}

function getCharJeunoFame($charid) {
	global $dbhost,$dbuser,$dbpass,$dbname;
	
	$sql = new SQL;
	$sql->connect($dbhost,$dbuser,$dbpass,$dbname);
	
	$query = $sql->query("SELECT SUM(fame_windurst+fame_sandoria+fame_bastok) FROM char_profile WHERE charid='$charid'");
	$res = $sql->fetch_assoc($query);
	
	return $res['SUM(fame_windurst+fame_sandoria+fame_bastok)'];
	$sql->close();
}

function getCharPoints($charid,$points) {
	global $dbhost,$dbuser,$dbpass,$dbname;
	
	$sql = new SQL;
	$sql->connect($dbhost,$dbuser,$dbpass,$dbname);
	
	switch ($points) {
		case "conquest":
			$name = "SUM(bastok_cp+windurst_cp+sandoria_cp)";
			$query = $sql->query("SELECT SUM(bastok_cp+windurst_cp+sandoria_cp) FROM char_points WHERE charid='$charid'");
			break;
		case "limit":
		case "imperial standing":
			$name = "imperial_standing";
			$query = $sql->query("SELECT imperial_standing FROM char_points WHERE charid='$charid'");
			break;
		case "leujaoam":
			$name = "leujaoam_assault_point";
			$query = $sql->query("SELECT leujaoam_assault_point FROM char_points WHERE charid='$charid'");
			break;
		case "lebros":
			$name = "lebros_assault_point";
			$query = $sql->query("SELECT lebros_assault_point FROM char_points WHERE charid='$charid'");
			break;
		case "ilrusi":
			$name = "ilrusi_assault_point";
			$query = $sql->query("SELECT ilrusi_assault_point FROM char_points WHERE charid='$charid'");
			break;
		case "mamool":
			$name = "mamool_assault_point";
			$query = $sql->query("SELECT mamool_assault_point FROM char_points WHERE charid='$charid'");
			break;
		case "periqia":
			$name = "periqia_assault_point";
			$query = $sql->query("SELECT periqia_assault_point FROM char_points WHERE charid='$charid'");
			break;
		case "nyzul":
			$name = "nyzul_assault_point";
			$query = $sql->query("SELECT nyzul_assault_point FROM char_points WHERE charid='$charid'");
			break;
		default:
			$name = "SUM(bastok_cp+windurst_cp+sandoria_cp)";
			$query = $sql->query("SELECT SUM(bastok_cp+windurst_cp+sandoria_cp) FROM char_points WHERE charid='$charid'");
			break; 
	}
	
	$res = $sql->fetch_assoc($query);
	
	if ($sql->num_rows($query) < 1) {
		return 0;
	}
	else {
		return $res[$name];
	}
	
	$sql->close();
}

function charMaxInventory($charid,$slot) {
	global $dbhost,$dbuser,$dbpass,$dbname;
	
	$sql = new SQL;
	$sql->connect($dbhost,$dbuser,$dbpass,$dbname);
	
	$query = $sql->query("SELECT $slot FROM char_storage WHERE charid='$charid'");
	$res = $sql->fetch_assoc($query);
	
	return $res[$slot];
	
	$sql->close();
}

// Misc Functions
function getZoneName($zoneid) {
	global $dbhost, $dbuser, $dbpass, $dspdb;
	
	$sql = new SQL;
	$sql->connect($dbhost,$dbuser,$dbpass,$dspdb);
	
	$query = $sql->query("SELECT name FROM zones WHERE id='$zoneid'");
	$res = $sql->fetch_assoc($query);
	
	if (empty($res['name'])) {
		return "Unknown Zone";
	}
	else {
		return $res['name'];
	}
	$sql->close();
}

function getJobName($job) {
	
	$j = array(
	'war' => 'Warrior',
	'mnk' => 'Monk',
	'whm' => 'White Mage',
	'blm' => 'Black Mage',
	'rdm' => 'Red Mage',
	'thf' => 'Thief',
	'pld' => 'Paladin',
	'drk' => 'Dark Knight',
	'bst' => 'Beastmaster',
	'brd' => 'Bard',
	'rng' => 'Ranger',
	'sam' => 'Samurai',
	'nin' => 'Ninja',
	'drg' => 'Dragoon',
	'smn' => 'Summoner',
	'blu' => 'Blue Mage',
	'cor' => 'Corsair',
	'pup' => 'Puppetmaster',
	'dnc' => 'Dancer',
	'sch' => 'Scholar',
	'' => ' ');
	
	return $j[$job];
}

function getItemName($itemid) {
	global $dbhost,$dbuser,$dbpass,$dbname;
	
	$sql = new SQL;
	$sql->connect($dbhost,$dbuser,$dbpass,$dbname);
	
	$query = $sql->query("SELECT name FROM item_basic WHERE itemid='$itemid'");
	$res = $sql->fetch_assoc($query);
	
	$item = $res['name'];
	
	$item = str_replace("_"," ",$item);
	
	return $item;
	$sql->close();
}

function getMaxXP($level) {
	global $dbhost,$dbuser,$dbpass,$dbname;
	
	$sql = new SQL;
	$sql->connect($dbhost,$dbuser,$dbpass,$dbname);
	
	$query = $sql->query("SELECT exp FROM exp_base WHERE level='$level'");
	$res = $sql->fetch_assoc($query);
	
	if ($sql->num_rows($query) < 1) {
		return 0;
	}
	else {
		return $res['exp'];
	}
	
	$sql->close();
}

function getRegionCount($region) {
	global $dbhost,$dbuser,$dbpass,$dbname;
	
	$sql = new SQL;
	$sql->connect($dbhost,$dbuser,$dbpass,$dbname);
	
	$q = "SELECT * FROM chars INNER JOIN accounts_sessions ON accounts_sessions.charid = chars.charid WHERE ";
	switch ($region) {
		case "Bastok":
			$cont = "pos_zone='246' OR pos_zone='234' OR pos_zone='235' OR pos_zone='237'";
			break;
		case "Sandoria":
			$cont = "pos_zone='230' OR pos_zone='231' OR pos_zone='232' OR pos_zone='233'";
			break;
		case "Ronfaure":
			$cont = "pos_zone='100' OR pos_zone='101' OR pos_zone='139' OR pos_zone='140' OR pos_zone='141' OR pos_zone='142' OR pos_zone='167' OR pos_zone='190'";
			break;
		case "Zulkheim":
			$cont = "pos_zone='102' OR pos_zone='103' OR pos_zone='108' OR pos_zone='193' OR pos_zone='196' OR pos_zone='248'";
			break;
		case "Windurst":
			$cont = "pos_zone='242' OR pos_zone='240' OR pos_zone='239' OR pos_zone='241' OR pos_zone='238'";
			break;
		case "Valdeaunia":
			$cont = "pos_zone='6' OR pos_zone=' 161' OR pos_zone='162' OR pos_zone='165' OR pos_zone='5' OR pos_zone='112'";
			break;
		case "Norvallen":
			$cont = "pos_zone='105' OR pos_zone='2' OR pos_zone='149' OR pos_zone='195' OR pos_zone='104' OR pos_zone='150' OR pos_zone='1'";
			break;
		case "Volbow":
			$cont = "pos_zone='113' OR pos_zone='201' OR pos_zone='212' OR pos_zone='174' OR pos_zone='128'";
			break;
		case "Sarutabaruta":
			$cont = "pos_zone='146' OR pos_zone='116' OR pos_zone='170' OR pos_zone='145' OR pos_zone='192' OR pos_zone='194' OR pos_zone='169' OR pos_zone='115'";
			break;
		case "ElshimoLL":
			$cont = "pos_zone='250' OR pos_zone='252' OR pos_zone='176' OR pos_zone='123'";
			break;
		case "ElshimoUL":
			$cont = "pos_zone='207' OR pos_zone='211' OR pos_zone='160' OR pos_zone='205' OR pos_zone='163' OR pos_zone='159' OR pos_zone='124'";
			break;
		case "Kuzotz":
			$cont = "pos_zone='209' OR pos_zone='114' OR pos_zone='168' OR pos_zone='208' OR pos_zone='247' OR pos_zone='125'";
			break;
		case "Tavnazian":
			$cont = "pos_zone='24' OR pos_zone='25' OR pos_zone='31' OR pos_zone='27' OR pos_zone='30' OR pos_zone='29'OR pos_zone='28' OR pos_zone='32' OR pos_zone='26'";
			break;
		case "Fauregandi":
			$cont = "pos_zone='207' OR pos_zone='211' OR pos_zone='160' OR pos_zone='205' OR pos_zone='163' OR pos_zone='159' OR pos_zone='124'";
			break;
		case "Qufim":
			$cont = "pos_zone='127' OR pos_zone='184' OR pos_zone='157' OR pos_zone='126' OR pos_zone='179' OR pos_zone='158'";
			break;
		case "Jeuno":
			$cont = "pos_zone='246' OR pos_zone='244' OR pos_zone='245' OR pos_zone='243'";
			break;
		case "Gustaberg":
			$cont = "pos_zone='191' OR pos_zone='173' OR pos_zone='106' OR pos_zone='143' OR pos_zone='107' OR pos_zone='144' OR pos_zone='172'";
			break;
		case "TuLia":
			$cont = "pos_zone='181' OR pos_zone='180' OR pos_zone='130' OR pos_zone='178' OR pos_zone='177'";
			break;
		case "LiTelor":
			$cont = "pos_zone='153' OR pos_zone='202' OR pos_zone='154' OR pos_zone='251' OR pos_zone='122' OR pos_zone='121'";
			break;
		case "Kolshushu":
			$cont = "pos_zone='4' OR pos_zone='44' OR pos_zone='118' OR pos_zone='213' OR pos_zone='3' OR pos_zone='198' OR pos_zone='249' OR pos_zone='117'";
			break;
		case "Aragoneu":
			$cont = "pos_zone='152' OR pos_zone='7' OR pos_zone='8' OR pos_zone='151' OR pos_zone='200' OR pos_zone='119' OR pos_zone='120'";
			break;
		case "Derfland":
			$cont = "pos_zone='147' OR pos_zone='197' OR pos_zone='109' OR pos_zone='148' OR pos_zone='110'";
			break;
		case "Movalpolos":
			$cont = "pos_zone='13' OR pos_zone='12' OR pos_zone='11'";
			break;
		default:
		$cont = "pos_zone='230' OR pos_zone='231'";
			break;
	}
		
	$query = $q.$cont;
	$res = $sql->query($query) or die($sql->error());
	if (empty($res)) {
		$ret = 'There are 0 players in this region';
	}
	else {
		$total = $sql->num_rows($res);
		$ret = 'There are '.$total.' players in this region';
	}
	return $ret;
	$sql->close();
}

function getRegionName($regionid) {
	
	switch ($regionid) {
		case 1:
			$name = "Bastok";
			break;
		case 2:
			$name = "San D'Oria";
			break;
		case 3:
			$name = "Ronfaure";
			break;
		case 4:
			$name = "Zulkhelm";
			break;
		case 5:
			$name = "Windurst";
			break;
		case 6:
			$name = "Valdeaunia";
			break;
		case 7:
			$name = "Norvallen";
			break;
		case 8:
			$name = "Volbow";
			break;
		case 9:
			$name = "Sarutabaruta";
			break;
		case 10:
			$name = "Elshimo Lowlands";
			break;
		case 11:
			$name = "Elshimo Upperlands";
			break;
		case 12:
			$name = "Kuzotz";
			break;
		case 13:
			$name = "Tavnazian";
			break;
		case 14:
			$name = "Fauregandi";
			break;
		case 15:
			$name = "Qufim";
			break;
		case 16:
			$name = "Jeuno";
			break;
		case 17:
			$name = "Gustaberg";
			break;
		case 18:
			$name = "Tu'Lia";
			break;
		case 19:
			$name = "Li'Telor";
			break;
		case 20:
			$name = "Kolshushu";
			break;
		case 21:
			$name = "Aragoneu";
			break;
		case 22:
			$name = "Derfland";
			break;
		case 23:
			$name = "Movalpolos";
			break;
		default:
			$name = "San D'Oria";
			break;
	}
	
	return $name;
}
?>